package com.ocean.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocean.admin.entity.SysPost;
import com.ocean.admin.entity.SysUserPost;
import com.ocean.admin.mapper.SysPostMapper;
import com.ocean.admin.model.dto.post.SysPostQueryDTO;
import com.ocean.admin.model.dto.post.SysPostSaveDTO;
import com.ocean.admin.model.dto.post.SysPostUpdateDTO;
import com.ocean.admin.model.vo.post.SysPostVo;
import com.ocean.admin.extension.security.core.SysUserDetails;
import com.ocean.admin.extension.security.filter.WebFilterExtend;
import com.ocean.admin.service.ISysPostService;
import com.ocean.admin.service.ISysUserPostService;
import com.ocean.core.exception.verify.ApplicationException;
import com.ocean.core.util.constants.ConstantsAttribute;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Autowired
    private ISysUserPostService iSysUserPostService;

    /**
     * 分页查询岗位列表
     *
     * @param queryDTO 查询条件
     * @return 结果
     */
    @Override
    public IPage<SysPostVo> pageList(SysPostQueryDTO queryDTO) {
        Page<SysPostVo> page = new Page<>(queryDTO.getPage(), queryDTO.getPageSize());
        IPage<SysPostVo> pageList = baseMapper.pageList(page, queryDTO);
        pageList.getRecords().forEach(res -> res.setStatusName(res.getStatus() == 1 ? "停用" : "正常"));
        return pageList;
    }

    /**
     * 新增岗位信息
     *
     * @param saveDTO 岗位信息
     * @return 结果
     */
    @Override
    public String savePost(SysPostSaveDTO saveDTO) {
        this.checkSavePost(saveDTO);
        SysPost post = new SysPost();
        BeanUtils.copyProperties(saveDTO, post);
        SysUserDetails current = WebFilterExtend.current();
        post.setCreateUserId(current.getUserId());
        post.setCreateUserName(current.getNickName());
        post.setUpdateUserId(current.getUserId());
        post.setUpdateUserName(current.getNickName());
        this.save(post);
        return ConstantsAttribute.INSERT_SUCCESS;
    }

    /**
     * 删除岗位
     *
     * @param postIds 岗位ID
     * @return 结果
     */
    @Override
    public String removePost(Long[] postIds) {
        List<Long> postIdList = Arrays.asList(postIds);
        this.checkRemovePost(postIdList);
        this.removeByIds(postIdList);
        return ConstantsAttribute.REMOVE_SUCCESS;
    }

    /**
     * 修改岗位信息
     *
     * @param updateDTO 岗位信息
     * @return 结果
     */
    @Override
    public String updatePost(SysPostUpdateDTO updateDTO) {
        this.checkUpdatePost(updateDTO);
        SysPost post = new SysPost();
        BeanUtils.copyProperties(updateDTO, post);
        SysUserDetails current = WebFilterExtend.current();
        post.setUpdateUserId(current.getUserId());
        post.setUpdateUserName(current.getNickName());
        this.updateById(post);
        return ConstantsAttribute.UPDATE_SUCCESS;
    }

    public void checkSavePost(SysPostSaveDTO saveDTO) {
        Long nameCount = baseMapper.selectCount(Wrappers.<SysPost>lambdaQuery()
                .eq(SysPost::getPostName, saveDTO.getPostName()));
        if (nameCount > 0) {
            throw new ApplicationException("岗位名已存在，请更换");
        }
        Long codeCount = baseMapper.selectCount(Wrappers.<SysPost>lambdaQuery()
                .eq(SysPost::getPostCode, saveDTO.getPostCode()));
        if (codeCount > 0) {
            throw new ApplicationException("岗位编码已存在，请更换");
        }
    }

    public void checkRemovePost(List<Long> postIds) {
        List<SysUserPost> sysUserPosts = iSysUserPostService.listByIds(postIds);
        if (sysUserPosts.size() > 0) {
            throw new ApplicationException("所选中的岗位中含有已分配的岗位，不能删除");
        }
    }

    private void checkUpdatePost(SysPostUpdateDTO updateDTO) {
        SysPost postName = baseMapper.selectOne(Wrappers.<SysPost>lambdaQuery()
                .eq(SysPost::getPostName, updateDTO.getPostName()));
        if (postName != null && !Objects.equals(postName.getPostId(), updateDTO.getPostId())) {
            throw new ApplicationException("修改失败，岗位名称已存在");
        }
        SysPost postCode = baseMapper.selectOne(Wrappers.<SysPost>lambdaQuery()
                .eq(SysPost::getPostCode, updateDTO.getPostCode()));
        if (postCode != null && !Objects.equals(postCode.getPostId(), updateDTO.getPostId())) {
            throw new ApplicationException("修改失败，岗位编码已存在");
        }
    }
}
